package com.example.springbott.service;

import com.example.springbott.bean.*;
import com.example.springbott.bean.common.CommonData;
import com.example.springbott.bean.common.PageInfoBase;
import com.example.springbott.mapper.*;
import com.example.springbott.util.CommonDataUtil;
import com.example.springbott.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Component
public class AdminUserServiceImpl implements AdminUserService{
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketAddressMapper addressMapper;
    @Autowired
    MarketFootprintMapper marketFootprintMapper;
    @Resource
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Resource
    MarketFeedbackMapper marketFeedbackMapper;
    @Autowired
    MarketCollectMapper marketCollectMapper;
    //--------------------------------------------------Kerbal--------------------------------------------------
    @Override
    public MarketUser adminUserDetail(Integer id) {
        return  userMapper.selectByPrimaryKey(id);
    }
    @Override
    public CommonData<MarketUser> adminUserList (Integer page, Integer limit, String sort, String order, String
            username, Integer mobile){
        PageHelper.startPage(page,limit);
        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.setOrderByClause(sort + " " + order);
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        } else if (mobile != null) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);
        PageInfo<MarketUser> marketUserPageInfo = new PageInfo<>(marketUsers);
        CommonData commonData = CommonDataUtil.CommonData(marketUserPageInfo);
        return commonData;
    }
    @Transactional
    @Override
    public int adminUserUpdate (MarketUser user){
        MarketUserExample marketUserExample = new MarketUserExample();
        return  userMapper.updateByPrimaryKey(user);
    }


    @Override
    public CommonData<MarketAddress> adminAddressList(PageInfoBase pageInfoBase , String name, Integer userId) {
        PageHelper.startPage(pageInfoBase.getPage(),pageInfoBase.getLimit());
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.setOrderByClause(pageInfoBase.getSort()+" "+pageInfoBase.getOrder());
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (name!=null){
            criteria.andNameEqualTo(name);
        }else if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(marketAddresses);
        return CommonDataUtil.CommonData(marketAddressPageInfo);

    }

    @Override
    public CommonData<MarketFootprint> adminFootPrintList(PageInfoBase pageInfoBase, Integer userId, Integer goodsId) {
        PageHelper.startPage(pageInfoBase.getPage(),pageInfoBase.getLimit());
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        marketFootprintExample.setOrderByClause(pageInfoBase.getSort()+" "+pageInfoBase.getOrder());
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }else if (goodsId!=null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<MarketFootprint> marketFootprints = marketFootprintMapper.selectByExample(marketFootprintExample);
        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(marketFootprints);
        CommonData commonData = CommonDataUtil.CommonData(marketFootprintPageInfo);
        return commonData;
    }


    /**
     * 该方法用于用户管理模块，搜索，展示用户历史记录
     * @param page
     * @param limit
     * @param keyword
     * @param sort
     * @param order
     * @return
     */
    @Override
    public CommonData<MarketSearchHistory> historyList(Integer page, Integer limit, String keyword, String sort, String order,Integer userId) {
        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        if (!StringUtils.isEmpty(keyword)){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (!(userId==null||"".equals(userId))){
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketSearchHistory> list = marketSearchHistoryMapper.selectByExample(historyExample);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(list);
        List<MarketSearchHistory> list1 = pageInfo.getList();
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        CommonData<MarketSearchHistory> data = new CommonData<MarketSearchHistory>((int) total, pages, limit, page,list1);
        return data;
    }


    /**
     * 该方法用于搜索展示，用户反馈
     * @param page
     * @param limit
     * @param username
     * @param sort
     * @param order
     * @param id
     * @return
     */
    @Override
    public CommonData<MarketFeedback> feedbackList(Integer page, Integer limit, String username, String sort, String order, Integer id) {
        MarketFeedbackExample feedbackExample = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (!StringUtils.isEmpty(username)){
            criteria.andUsernameLike(username);
        }
        if (!(id==null||"".equals(id))){
            criteria.andIdEqualTo(id);
        }
        List<MarketFeedback> list = marketFeedbackMapper.selectByExample(feedbackExample);
        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(list);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        List<MarketFeedback> list1 = pageInfo.getList();
        CommonData<MarketFeedback> data = new CommonData<MarketFeedback>((int) total, pages, limit, page, list1);
        return data;
    }

    @Override
    public CommonData adminCollectList(PageInfoBase pageInfoBase, Integer userId, Integer valueId) {
        PageHelper.startPage(pageInfoBase.getPage(),pageInfoBase.getLimit());
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();


        criteria.andDeletedEqualTo(false);
        if (userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId!=null){
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(marketCollects);
        return CommonData.data(marketCollectPageInfo);


    }

//--------------------------------------------------Kerbal--------------------------------------------------
}
